package be.kuleuven.mume.paginas;

import java.util.ArrayList;
import java.util.List;


import be.kuleuven.mume.R;
import be.kuleuven.mume.arrayAdapters.UpdatesArrayAdapter;
import be.kuleuven.mume.framework.Update;
import be.kuleuven.mume.framework.Vak;
import be.kuleuven.mume.hulpklassen.DataFetcher;
import be.kuleuven.mume.hulpklassen.JSONParser.UpdatesParser;
import be.kuleuven.mume.hulpklassen.TwitterHelper;
import be.kuleuven.mume.listProxys.LastCellLoadingProxy;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

public class VakUpdatesPagina extends Activity{
	//private static String TAG = "VakUpdatesPagina";
	
	private final int TWEET_ID = 0;
    private Vak vak;
    private UpdatesArrayAdapter updatesAdapter;
    private LastCellLoadingProxy loadingProxy;
    private DataFetcher<Update> datafetcher;
    private List<Update> updates;
    private TwitterHelper twitterH;
    private ListView table;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);  	  	
    	setContentView(R.layout.vakupdates);
    	
    	restoreBundle(savedInstanceState);
    	tryLoadingData();
    	//mDbHelper = new DbAdapter(this);
    	
    	
    	table = (ListView)findViewById(R.id.updatestable);
    	
    	loadingProxy = new LastCellLoadingProxy(updatesAdapter);
    	loadingProxy.noDataString_ID = R.string.geen_updates;
    	
    	table.setAdapter(loadingProxy);
    	
    	if(vak != null)
    		initiateDataFetcher();
    		
    	twitterH = new TwitterHelper(this.getBaseContext(), this);
    	twitterH.checkTwitterPermissions(false, vak.getVakId());
    }
	
	private void initiateDataFetcher() {
    	if(vak.getHashTag() != null && vak.getHashTag() != "") {
	    	String url = Update.URL + "?hashtag=" + vak.getHashTag();
	    	UpdatesParser parser = new UpdatesParser();
	    	datafetcher = new DataFetcher<Update>(parser, url, table, updates, this.getBaseContext()){
	    		public void DataReceived(){
	    			if(!datafetcher.hasMoreData())
	    				loadingProxy.setIsLoading(false);
	    			updatesAdapter.notifyDataSetChanged();
	    		}
	    	};
    	} else {
			loadingProxy.setIsLoading(false);
			updatesAdapter.notifyDataSetChanged();
    	}
	}

	private void tryLoadingData() {
		Object o = getLastNonConfigurationInstance();
		if(o == null) {
			Bundle extras = getIntent().getExtras();
			if(vak == null && extras != null)
				vak = (Vak)extras.get("VAK");
    		updates = new ArrayList<Update>();
			updatesAdapter = new UpdatesArrayAdapter(this, updates);
		} else {
			VakUpdatesData data = (VakUpdatesData)o;
			data.load(this);
		}
	}

	@Override
	public Object onRetainNonConfigurationInstance() {
		VakUpdatesData data = new VakUpdatesData();
		data.storeData(this);
		return data;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);	
		menu.add(0,TWEET_ID,0,R.string.tweet);
		return true;
	}
	
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case TWEET_ID:
        	if(twitterH.checkTwitterPermissions(true, vak.getVakId()))
        			twitterH.showInputDialog();
        	else if(twitterH.getState().equals(TwitterHelper.State.verifying)) {
        		twitterH.showIsverified = true;
        		Toast.makeText(this, R.string.twitter_verifying, 5).show();
        	}
            return true;
        }
        return false;
    }

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		restoreBundle(savedInstanceState);
	}

	private void restoreBundle(Bundle savedInstanceState) {
		if(savedInstanceState != null && savedInstanceState.containsKey(Vak.KEY_ID)){
			this.vak = (Vak)savedInstanceState.get(Vak.KEY_ID);
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putSerializable(Vak.KEY_ID, vak);
	}
	
	protected static class VakUpdatesData {
    	private List<Update> updates;
    	private Vak vak;
    	private UpdatesArrayAdapter adapter;
    	
    	public void storeData(VakUpdatesPagina pag){
    		updates = pag.updates;
    		vak = pag.vak;
    		adapter = pag.updatesAdapter;
    	}
    	
    	public void load(VakUpdatesPagina pag){
    		pag.updates = updates;
    		pag.vak = vak;
    		pag.updatesAdapter = adapter;
    	}
    }
}
